<?php
if(isset($_POST['add_item'])){
	if(!empty($_POST['name']) && !empty($_POST['stock']) && is_numeric($_POST['stock'])){
		/* CHECK `name` if has duplicate */
		$tmp = mysql_query("SELECT `itemid` FROM `items` WHERE `name` = '".$_POST['name']."'");
		if(mysql_num_rows($tmp)==0){
			/* INSERT */
			if(
			mysql_query("INSERT INTO `items`(`name`,`stock`,`description`) VALUES ('".$_POST['name']."',".$_POST['stock'].",'".$_POST['description']."')") && 
			mysql_query("INSERT INTO `item_logs`(`itemid`,`qty`,`details`,`date`,`action`) VALUES ('".mysql_insert_id()."',".$_POST['stock'].",'~ Initial stock',NOW(),'IN')")
			){
				echo '<div class="success">Create Success</div>';
			}
			else echo '<div class="failed">Create Failed</div>';
		}
		else echo '<div class="failed">Name should be <strong>UNIQUE</strong></div>';
	}
	else echo '<div class="failed">One of the fields is <strong>empty</strong> OR<br>invalid input for <strong>Stock</strong></div>';
}


?>
<form name="form1" method="post" action="">
<table align="center" width="400" border="0">
  <tr>
    <th colspan="2">&nbsp;</td>
  </tr>
  <tr>
    <td><label for="name">Item Name</label></td>
    <td><label for="name"></label>
    <input name="name" type="text" id="name" value="<?=isset($_POST['name'])?$_POST['name']:""?>"></td>
  </tr>
  <tr>
    <td><label for="stock">Stock</label></td>
    <td><input name="stock" type="text" id="stock" size="3" value="<?=isset($_POST['stock'])?$_POST['stock']:""?>"></td>
  </tr>
  <tr>
    <td><label for="description">Description</label></td>
    <td>
      <label for="description"></label>
      <textarea name="description" id="description" cols="45" rows="5"><?=isset($_POST['description'])?$_POST['description']:""?></textarea>
    </td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><button type="submit" name="add_item"><strong>Submit</strong></button></td>
  </tr>
</table>
</form>
